# from asyncio.windows_events import NULL
import requests,re,json,html2text,sys,time,urllib.parse
from bs4 import BeautifulSoup
from array import array
import time 
from urllib.request import urlretrieve
import os
import shelve
import aiohttp
import asyncio
from datetime import datetime
from functools import wraps
import random
import os.path
import logging
from logging.handlers import TimedRotatingFileHandler
from logging.handlers import RotatingFileHandler
from urllib.parse import quote,unquote
import html
import json
import sys

class WeiboVideoFetcher():
    def __init__(self, headerFile):
        self.headerFile = headerFile
        self.get_header_from_file()
        self.get_cookies_from_file()
        self.setVideoDefList()
        self.setPicDefList()
        self.fileIdx = 0

    def setVideoDefList(self):
        self.videoDefList = [
            "hevc_mp4_720p",
            "mp4_720p_mp4",
            "inch_5_5_mp4_hd",
            "inch_5_mp4_hd",
            "inch_4_mp4_hd",
            "stream_url_hd",
            "stream_url"
        ]

    def setPicDefList(self):
        self.picDefList = [
            "largest",
            "original",
            "large",
            "mw2000"
        ]

    def getVideoUrl(self, data):
        if 'media_info' in data:
            for i in self.videoDefList:
                if i in data['media_info'] and len(data['media_info'][i]):
                    return data['media_info'][i]

    def getPicUrl(self, data, currentPic):
        for k, v in data.items():
            for i in self.picDefList:
                if i in v:
                    currentPic.append(v[i]["url"])
                    break

    def get_target_meida_list(self, targetIndex):
        since_id = ""
        url = ""
        videoList = []
        picList = []
        for i in range(0, targetIndex):
            try:
                if since_id == "":
                    url = "https://weibo.com/ajax/statuses/mymblog?uid=%s&page=1&feature=0" % (self.uid)
                else:
                    url = "https://weibo.com/ajax/statuses/mymblog?uid=%s&page=%d&feature=0&since_id=%s" % (self.uid, i + 1, since_id)
                response = requests.get(url, headers = self.header, cookies = self.cookies)
                data = json.loads(str(response.content, encoding='utf-8'))
                since_id = data['data']["since_id"]
                if i == targetIndex - 1:
                    currentVideo = [self.getVideoUrl(j['page_info']) for j in data['data']["list"] if 'page_info' in j]
                    currentPic = []
                    [self.getPicUrl(j['pic_infos'], currentPic) for j in data['data']["list"] if 'pic_infos' in j]
                    print([j['created_at'] for j in data['data']["list"] if 'created_at' in j])
                    videoList.extend(currentVideo)
                    picList.extend(currentPic)
            except Exception:
                print("error")
        return videoList, picList

    def get_header_from_file(self):
        header = {}
        with open(self.headerFile, 'r') as f:
            for i in f.readlines():
                header[i.split(':')[0].strip().lower()] = i.split(':')[1].strip()
        for k in list(header.keys()):
            if k.startswith(':') or k == 'cookie' or len(k) == 0:
                del header[k]
        self.header = header

    def get_cookies_from_file(self):
        with open(self.headerFile, 'r') as f:
            for i in f.readlines():
                if i.startswith('cookie') or i.startswith('Cookie'):
                    self.cookies = {x.strip().split('=')[0] : x.strip().split('=')[1] for x in i[i.find(':') + 1 : ].strip().split(';')}
                    break

    def get_blogger_uid_by_name(self, name, uid):
        url = 'https://weibo.com/aj/relation/attention?ajwvr=6&q=' + name
        get_url = requests.get(url, headers = self.header, cookies = self.cookies)
        codingTypr = get_url.encoding
        text = get_url.text.encode(codingTypr, errors='ignore').decode('utf-8', errors='ignore')
        result = json.loads(text)
        uidList = [x["uid"] for x in result["data"]]
        print(uidList)
        if len(uidList) > 0:
            self.uid = uidList[0]
        else:
            self.uid = uid
        obj.baseUrl = "https://weibo.com/u/" + self.uid
        print(obj.baseUrl)


if __name__ == "__main__":
    videoResult = {}
    imgList = set()
    dbName = 'weibo_recorder'

    startIdx = 1
    endIdx = 5
    postName = ''
    uid = sys.argv[1] if len(sys.argv) > 1 else "5553544144"
    print(uid)

    # postName = '亲亲奥利给'
    # postName = '艺装堂'
    # postName = 'AF-FEATUREDQUALITYRELEASE捌捌V'
    # postName = '写真store'
    # postName = '小牧民sp'
    # postName = '艺术舞'
    # postName = '小小迷妹热舞'
    # postName = '可叔'
    # postName = '宸嘟嘟'
    # postName = '业界鉴赏官'
    # postName = '许爱凌'
    # postName = 'CuteFaccce'
    # postName = '格鲁日海森'
    # postName = '鹿玖仙A'
    # postName = '纯良软妹'
    # postName = '优雅的韩女主'
    # postName = '迷路的小兰亭so'
    # postName = '糖果超甜bot'
    # postName = '勤奋顺7自然'
    # postName = '呀杨羽然'
    # postName = 'AfreecaTV舞蹈录制'
    # postName = 'AF高质量视频'
    # postName = 'AF-FEATUREDQUALITYRELEASE捌捌V'
    # postName = '真诚录制'
    # postName = '录像站'
    # postName = '中国腿模网'
    # postName = '超模御姐'
    # postName = '雨的精灵'
    # postName = '艾尚街拍'
    # postName = '小牧民sp'
    # postName = '直播舞娇娘'
    # postName = 'VPlus女神'
    # postName = '天菜姐'
    # postName = '颜冰謦'
    # postName = 'Tiger凌云'
    # postName = 'af尼美舞蹈'
    # postName = '街个拍'
    # postName = '传奇女司机'
    # postName = '冒牌四郎'
    # postName = '人类高质量女郎'
    # postName = '有妹纸学院'
    # postName = 'AfreecaTV精选'
    # postName = '张曼淋'

    obj = WeiboVideoFetcher('header.txt')
    obj.get_blogger_uid_by_name(postName, uid)

    videoResult = []
    picResult = []
    for i in range(startIdx, endIdx + 1):
        print(f'start get page {i}')
        videoResultTmp, picResultTmp = obj.get_target_meida_list(i)
        videoResult.extend([x for x in videoResultTmp if x is not None])
        picResult.extend([x for x in picResultTmp if x is not None])

    dbase = shelve.open(dbName)
    videoList = {}
    if 'video_list' in dbase:
        videoList = dbase['video_list']
    weiboId = obj.uid
    videoListToPrint = []
    if weiboId in videoList:
        for i in videoResult:
            key = i[7:i.find('?')]
            if key not in videoList[weiboId]:
                videoList[weiboId][key] = i
                videoListToPrint.append(i)
    else:
        videoListToPrint = videoResult
        videoList[weiboId] = {x[7:x.find('?')] : x for x in videoResult}
    
    if 'img_list' in dbase:
        imgList = set(dbase['img_list'])
    imgListToPrint = []
    for i in picResult:
        if i not in imgList:
            imgListToPrint.append(i)
    dbase['video_list'] = videoList
    dbase['img_list'] = imgList.union(set(imgListToPrint))
    dbase.close()

    with open('result.txt', 'w', encoding = 'utf-8') as f:
        for i in set(videoListToPrint):
            f.write(i + '\n')

    with open('result_img.txt', 'w', encoding = 'utf-8') as f:
        for i in set(imgListToPrint):
            f.write(i + '\n')